package com.genesis.yunnanji.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.Help;
import com.genesis.yunnanji.activity.Login;
import com.genesis.yunnanji.activity.MyCollect;
import com.genesis.yunnanji.activity.MyComment;
import com.genesis.yunnanji.activity.MyInfo;
import com.genesis.yunnanji.activity.Setting;
import com.genesis.yunnanji.activity.WebActivity;
import com.genesis.yunnanji.bean.PersonBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/11/1.
 */
@ContentView(R.layout.fragment_personal)
public class Personal extends BaseFragment {
    private Intent intent;
    @ViewInject(R.id.bt_personal_login)
    private TextView mLogin;
    @ViewInject(R.id.ll_personal_basic)
    private RelativeLayout rlMall;
    @ViewInject(R.id.ll_personal_mycomment)
    private RelativeLayout rlComment;
    @ViewInject(R.id.ll_personal_setting)
    private RelativeLayout rlSetting;
    @ViewInject(R.id.civ_personal_header)
    private CircleImageView circleImageView;
    @ViewInject(R.id.ll_personal_mycollect)
    private RelativeLayout rlCollect;
    @ViewInject(R.id.ll_personal_aboutus)
    private RelativeLayout rlAboutus;
    @ViewInject(R.id.ll_personal_help)
    private RelativeLayout rlHelp;
    @ViewInject(R.id.tv_personal_name)
    private TextView tvName;
    BitmapUtils bitmapUtils;

    public static Personal newInstance(String param) {
        Personal personal = new Personal();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        personal.setArguments(bundle);
        return personal;
    }


    @Override
    protected void initView() {
        bitmapUtils = new BitmapUtils(context);
        if (genesisUtils.isLogin()) {
            mLogin.setVisibility(View.GONE);
            tvName.setVisibility(View.VISIBLE);
            getPersonInfo();
        } else {
            mLogin.setVisibility(View.VISIBLE);
            tvName.setVisibility(View.GONE);
        }
    }

    @Override
    protected void setUpView() {

    }

    @Event(value = {R.id.bt_personal_login, R.id.ll_personal_basic, R.id.ll_personal_mycollect, R.id.ll_personal_setting, R.id.ll_personal_aboutus, R.id.ll_personal_help, R.id.ll_personal_mycomment}, type = View.OnClickListener.class)
    private void click(View v) {
        switch (v.getId()) {
            case R.id.bt_personal_login:
                intent = new Intent(context, Login.class);
                startActivity(intent);
                break;
            case R.id.ll_personal_basic:
                if (genesisUtils.isLogin()) {
                    intent = new Intent(context, MyInfo.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(context, Login.class);
                    startActivity(intent);
                }
                break;
            case R.id.ll_personal_mycomment:
                if (genesisUtils.isLogin()) {
                    intent = new Intent(context, MyComment.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(context, Login.class);
                    startActivity(intent);
                }
                break;
            case R.id.ll_personal_setting:
                intent = new Intent(context, Setting.class);
                startActivity(intent);
                break;
            case R.id.ll_personal_aboutus://关于我们
//                intent = new Intent(context, AboutUs.class);
//                startActivity(intent);
                intent = new Intent(context, WebActivity.class);
                intent.putExtra("url", "http://m.jinzhanji.com/AboutUs/details/id/98.html");
                intent.putExtra("news_id", "");
                startActivity(intent);
                break;
            case R.id.ll_personal_help://帮助中心
                intent = new Intent(context, Help.class);
                startActivity(intent);
                break;
            case R.id.ll_personal_mycollect:
                if (genesisUtils.isLogin()) {
                    intent = new Intent(context, MyCollect.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(context, Login.class);
                    startActivity(intent);
                }
                break;
        }

    }

    private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(PasswordLogin.LOGIN_STATUS)) {
                if (intent.getStringExtra("msg").equals("login")) {
                    getPersonInfo();
                    mLogin.setVisibility(View.GONE);
                    tvName.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("msg").equals("headicon")) {
                    getPersonInfo();
                    bitmapUtils.display(circleImageView, GenesisApiConfig.PIC_HOST + genesisUtils.getUserImage());
                } else if (intent.getStringExtra("msg").equals("logout")) {
                    BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
                    displayConfig.setLoadFailedDrawable(getResources().getDrawable(R.mipmap.defaultavatar));
                    bitmapUtils.display(circleImageView, GenesisApiConfig.PIC_HOST + genesisUtils.getUserImage(), displayConfig);

                    mLogin.setVisibility(View.VISIBLE);
                    tvName.setVisibility(View.GONE);
                }

            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        IntentFilter filter_dynamic = new IntentFilter();
        filter_dynamic.addAction(PasswordLogin.LOGIN_STATUS);
        context.registerReceiver(dynamicReceiver, filter_dynamic);
    }


    private void getPersonInfo() {
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PERSON_INFO);
        params.addBodyParameter("__apptoken__", genesisUtils.getAppToken());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                PersonBean personBean = gson.fromJson(result, PersonBean.class);
                genesisUtils.setUserProfile(personBean);
                genesisUtils.setUserImage(personBean.getResult().getInfo().getPhoto());
                genesisUtils.setIssetpwd(personBean.getResult().getInfo().getIssetpwd());
                BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
                displayConfig.setLoadFailedDrawable(getResources().getDrawable(R.mipmap.defaultavatar));
                bitmapUtils.display(circleImageView, GenesisApiConfig.PIC_HOST + personBean.getResult().getInfo().getPhoto(), displayConfig);
                tvName.setText(personBean.getResult().getInfo().getNickname());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
